import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import edu.rit.pj.Comm;

class newparallelimagechangecode

{
	static long time1,time2;
	public static void handlepixels(Image img, int x, int y, int w, int h) 
	{
		int[] pixels = new int[w * h];
		PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
		try 
		{
		    pg.grabPixels();
		} 
		catch (InterruptedException e) 
		{
		    System.err.println("interrupted waiting for pixels!");
		    return;
		}
		if ((pg.getStatus() & ImageObserver.ABORT) != 0) 
		{
		    System.err.println("image fetch aborted or errored");
		    return;
		}
		
		int [] newpixels = new int[w * h];
		int [] flippixels = new int[w * h];
		int [] blackpixels = new int[w * h];
				
		time1=System.currentTimeMillis();		
		for (int j = 0; j < h; j++) 
		{
 
		    for (int i = 0; i < w; i++) 
		    {
		    	flippixels[j * w + i] = pixels[j * w + (w - i - 1)];
			 	int pixel =  pixels[j * w + i];
			 	int red   = (pixel >> 16) & 0xff;
				int green = (pixel >>  8) & 0xff;
				int blue  = (pixel      ) & 0xff;
				
				int blackwhite = (int) (red*0.3 + green*0.59+ blue*0.11);//red + green + blue;		
				blackpixels[j * w + i] = (blackwhite << 16) | (blackwhite <<  8) | (blackwhite);
				newpixels[j * w + i] = ((255-red) << 16) | ((255-green) <<  8) | (255-blue);
		    }
		}
		
		time2=System.currentTimeMillis();		
		 
		System.out.println((time2-time1)+" msec");
		
		BufferedImage pixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);    
	    pixelImage.setRGB(0, 0, w, h, blackpixels, 0, w);
	    
	    // Use a label to display the image
	    JFrame frame2 = new JFrame("Image2");
	    JLabel label2 = new JLabel(new ImageIcon(pixelImage));
	    frame2.getContentPane().add(label2, BorderLayout.CENTER);
	    frame2.pack();
	    frame2.setVisible(true);
	    
	    
		BufferedImage newpixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);    
	    newpixelImage.setRGB(0, 0, w, h, newpixels, 0, w);
	    
	    // Use a label to display the image
	    JFrame frame3 = new JFrame("Image3");
	    JLabel label3 = new JLabel(new ImageIcon(newpixelImage));
	    frame3.getContentPane().add(label3, BorderLayout.CENTER);
	    frame3.pack();
	    frame3.setVisible(true);
	    
	    
	    BufferedImage flippixelImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);    
	    flippixelImage.setRGB(0, 0, w, h, flippixels, 0, w);
	    
	    // Use a label to display the image
	    JFrame frame4 = new JFrame("Image4");
	    JLabel label4 = new JLabel(new ImageIcon(flippixelImage));
	    frame4.getContentPane().add(label4, BorderLayout.CENTER);
	    frame4.pack();
	    frame4.setVisible(true);
	    
	 }
 

 public static void main(String args[]) throws IOException
 {
	 Comm.init(args);
 
 Image image = null;
 int height = 0, width = 0;
 try {
    
      
     URL url = new URL("http://www.nurseryclass.com/images/Twenty20-twenty%2020%20world%20cup-%20India-2007.jpg");
     image = ImageIO.read(url);
     time1=System.currentTimeMillis();		
     
     height = image.getHeight(null);
     width = image.getWidth(null);

 } 
 catch (IOException e) 
 {
 }
 

 // Use a label to display the image
  JFrame frame = new JFrame("Image1");
 JLabel label = new JLabel(new ImageIcon(image));
 frame.getContentPane().add(label, BorderLayout.CENTER);
 frame.pack();
 frame.setVisible(true);
 
 handlepixels(image, 0, 0, width, height);
 
 
 }
  
}
